using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyDatTour
{
    public partial class TimKiemTour : Form
    {
        public TimKiemTour()
        {
            InitializeComponent();
        }

        private void TimKiemTour_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("sp_LayDanhSachGaDi", DataProvider.Connect());
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cmbGaDi.Items.Add(dr["GaDi"].ToString());
            }
            cmd = new SqlCommand("sp_LayDanhSachGaDen", DataProvider.Connect());
            cmd.CommandType = CommandType.StoredProcedure;
           dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cmbGaDen.Items.Add(dr["GaDen"].ToString());
            }
            dr.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            
            String gaDi = cmbGaDi.Text;
            String gaDen = cmbGaDen.Text;
            DateTime ngayDi = dateTimePicker1.Value;
            SqlCommand cmd = new SqlCommand("sp_TimKiemTour_TheoGaDi_GaDen_NgayDi", DataProvider.Connect());
            // add parameter
            SqlParameter pm_gaDi = new SqlParameter("@gadi", SqlDbType.VarChar);
            pm_gaDi.Value = gaDi;
            cmd.Parameters.Add(pm_gaDi);

            SqlParameter pm_gaDen = new SqlParameter("@gaden", SqlDbType.VarChar);
            pm_gaDen.Value = gaDen;
            cmd.Parameters.Add(pm_gaDen);

            SqlParameter pm_NgayDi = new SqlParameter("@ngaydi", SqlDbType.DateTime);
            pm_NgayDi.Value = ngayDi.ToShortDateString();
            cmd.Parameters.Add(pm_NgayDi);
            
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();

            int i=0;
            while (dr.Read())
            {
                dataGridView1.Rows.Add();
              //  MessageBox.Show(dr["MaTour"].ToString());
                String maTour = dr["MaTour"].ToString();
                dataGridView1.Rows[i].Cells[0].Value = dr["TenTour"];
                dataGridView1.Rows[i].Cells[1].Value = dr["NgayKhoiHanh"];
                
                // lay so cho con trong
                cmd = new SqlCommand("sp_TimKiemTour_TheoMaTour", DataProvider.Connect());
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter pm_maTour = new SqlParameter("@matour", SqlDbType.VarChar);
                pm_maTour.Value = maTour;
                cmd.Parameters.Add(pm_maTour);

                SqlParameter returnValue = new SqlParameter("@soChoTrong", SqlDbType.Int);
                returnValue.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(returnValue);

                cmd.ExecuteNonQuery();
                int soChoTrong = Convert.ToInt32(returnValue.Value);
                dataGridView1.Rows[i].Cells[2].Value = soChoTrong ;
                

                i++;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DatTourMoi dtm = new DatTourMoi();
            dtm.Show();
        }

       
    }
}